<?php
namespace addons\yexam\service;


use app\admin\model\yexam\Answer;
use app\admin\model\yexam\Fav;
use think\Exception;

class Question
{

    public $model;

    public function __construct()
    {
        $this->model = new \app\admin\model\yexam\Unit();
    }

    /**
     * 获取章节练习答题卡
     */
    public function getTestCard($unit_id,$user_id){
        $questionModel = new \app\admin\model\yexam\Question();
        $data = $questionModel
            ->alias("question")
            ->field("question.*,ifnull(log.state,3) as state")
            ->join("yexam_question_log log","question.id=log.question_id and log.user_id=".$user_id,"left")
            ->where(['question.unit_id'=>$unit_id])->order("question.type asc,question.id asc")->select();

        $return = [];
        foreach($data as $v){
            $_return['id'] = $v['id'];
            $_return['state'] = $v['state'] ==3?3:($v['state']==0?1:2);        //1是错2是对3是未答过此题

            $return[] = $_return;

        }

        return $return;
    }


    /**
     * 收藏题目
     * @param $id
     * @param $user_id
     * @return int|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException]
     */
    public function favQuestion($id,$user_id){
        $questionModel = new \app\admin\model\yexam\Question();
        if (!$questionInfo = $questionModel->where(['id'=>$id])->find()) {
            return;
        }

        $favModel = new Fav();
        if($favInfo = $favModel->where(['user_id'=>$user_id,'question_id'=>$questionInfo['id']])->find()){
            $favInfo->delete();
            return 1;
        }else{
            $favModel = new Fav();
            $favModel->save([
                'user_id' => $user_id,
                'subject_id' => $questionInfo['subject_id'],
                'library_id' => $questionInfo['library_id'],
                'unit_id' => $questionInfo['unit_id'],
                'question_id' => $questionInfo['id'],
                'createtime' => time()
            ]);

            return 2;
        }
    }


    /**
     * 获取题目详情
     * @throws Exception
     */
    public function getQuestion($question_id,$user_id){

        $questionModel = new \app\admin\model\yexam\Question();
        $question = $questionModel->field("id,question_name,type,right_answer,area")->where(['id'=>$question_id])->find();
        if(empty($question)){
            return [];
        }else{
            //获取答案
            if($question['type'] == 3){
                $question['answers'] = array(
                    '0' => array(
                        'answer' => "对",
                        'answer_code' => "1",
                        'id' => 1,
                        'question_id' => $question['id']
                    ),
                    '1' => array(
                        'answer' => "错",
                        'answer_code' => "0",
                        'id' => 2,
                        'question_id' => $question['id']
                    ),

                );
            }else{
                $answerModel = new Answer();
                $question['answers'] = $answerModel->where(['question_id'=>$question['id']])->select();
            }

            $favModel = new Fav();
            if($favModel->where(['question_id'=>$question['id'],'user_id'=>$user_id])->count()){
                $question['is_fav'] = 1;
            }else{
                $question['is_fav'] = 0;
            }

        }

        return $question;
    }



}